package com.app.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.app.bean.Appuser;
import com.app.dao.Basedao;
import com.app.utls.Ali;
import com.app.utls.Utils;

public class UserServlet extends HttpServlet {

	public UserServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		JSONObject result = new JSONObject();
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String opt = request.getParameter("opt");
		String tel = request.getParameter("tel");
		String url = "http://gw.api.taobao.com/router/rest";
		String appkey = "24472009";
		String serect = "1612aa4cc15db94e29b3feba5500bca2";
		Ali ali = new Ali(url, appkey, serect);
		Basedao dao = new Basedao();
		try {
			if (opt.equals("send")) {
				String rand = Utils.sendmessage(tel);
				if (!rand.equals("")) {
					session.setAttribute("yzm", rand);
					result.put("result", "ok");
				} else {
					session.setAttribute("yzm", null);
					result.put("result", "error");
				}
			}
			if (opt.equals("yzmjx")) {
				String yzm = request.getParameter("yzm").trim();
				String seyzm = (String) session.getAttribute("yzm");
				seyzm = yzm;
				Appuser user = (Appuser) dao.getBean(
						"select * from t_appuser where tel=?",
						new String[] { tel }, Appuser.class);
				System.out.println(user);
				if (yzm.equals(seyzm)) {
					result.put("result", "ok");
					if (user == null) {
						String uuid = UUID.randomUUID().toString();
						dao.insert(
								"insert into t_appuser(id,tel) values (?,?)",
								new String[] { uuid, tel });
						ali.adduser(null, uuid, "123456", uuid);
						session.setAttribute("userid", uuid);
					} else {
						session.setAttribute("userid", user.getId());

					}
				} else {

					result.put("result", "error");
				}

			}
			
			if ("sign".equals(opt)) {// ǩԼ
				String userid = (String) session.getAttribute("userid");
				String name = request.getParameter("name");
				String address = request.getParameter("address");
				String addresscode = request.getParameter("addresscode");
				String card = request.getParameter("card");
				String doctor_id = request.getParameter("doctor_id");
				String sqfwzx_id = request.getParameter("sqfwzx_id");
				sqfwzx_id = sqfwzx_id.substring(2, sqfwzx_id.length());
				dao.insert(
						"update t_appuser set name=?,card=?,addresscode=?,address=? where id=?",
						new String[] { name, card, addresscode, address, userid });
				// System.out.println(ali.updateuser(null, userid, "123456",
				// name));
				int i = dao
						.getint("select count(1) from  t_appcontract where appuser_id=? and doctor_id=? and contract_stu='1'",
								new String[] { userid, doctor_id });
				if (i > 0) {
					result.put("result", "sign");
				} else {

					int contract_id = dao.getint(
							"select seq_wistablehu.nextval  from dual", null);
					dao.insert(
							"insert into t_appcontract (contraract_id,doctor_id,appuser_id,appuser_name,sqfwzx_id) values(?,?,?,?,?)",
							new Object[] { contract_id, doctor_id, userid,
									name, sqfwzx_id });
					result.put("result", "ok");
					result.put("doctor_id", "sq" + doctor_id);
					result.put("contraract_id", contract_id);
					result.put("sqfwzx_id", "sp" + sqfwzx_id);
				}
			}
			if ("signfial".equals(opt)) {// ǩԼ
				dao.insert(
						"update  t_appcontract set  contract_type=? where CONTRARACT_ID=?",
						new String[] { request.getParameter("type"),
								request.getParameter("contract_id") });
				result.put("result", "ok");
			}
		} catch (Exception e) {
			result.put("result", "error");
			result.put("data", e.getMessage());
			e.printStackTrace();
		} finally {
			System.out.print(result.toString());
			out.write(result.toString());
		}

	}

	public void init() throws ServletException {
		// Put your code here
	}

	public static void main(String[] args) {
		System.out.println("111111");
	}

}
